<?php
namespace app\houtai\controller\user\family;
use app\houtai\controller\BaseController;
use org\GoApi\FamilyCtrl;

class RangeController extends BaseController {
  /**
   *  @brief  : 查看/修改
   *  @return : page|JSON
   *  @details:
   */
  public function detailAction() {
    $GroupID = $this->request->param('GroupID/d', 0);
    if ($this->request->isGet()) { // `读取/新增`模板
      $conf = $GroupID > 0 ? FamilyCtrl::Read($GroupID) : [];
      return $this->fetch('', ['GroupID' => $GroupID, 'Conf' => $conf]);
    } else {
      ////////////////////////////////////
      if ($GroupID <= 0) {
        return result([], 10, 'GameID不能为负数');
      }
      $LimitMin = $this->request->param('LimitMin/d', 0);
      $LimitMax  = $this->request->param('LimitMax/d', 0);
      ////////////////////////////////////
      $family = table('Family', 'rydb.websms')->where('Code', $GroupID)->find();
      if (empty($family) || $family['State'] != 1) {
        return result([], 12, 'family不存在');
      }
      ////////////////////////////////////
      $eax = FamilyCtrl::Save($GroupID, $LimitMin, $LimitMax);
      return result([], 0);
    }
  }
  /**
   *  @brief  : 删除操作
   *  @return : JSON
   *  @details:
   */
  public function deloneAction() {
    $GroupID = $this->request->param('GroupID/d', 0);
    if ($GroupID <= 0) {
      return result([], 10, 'GameID不能为负数');
    }
    $limit = $GroupID > 0 ? FamilyCtrl::Read($GroupID) : [];
    if (empty($limit)) {
      return result([], 11, '区间不存在');
    }
    $eax = FamilyCtrl::Remove($GroupID);
    return result([], $eax);
  }
  /***
   * @brief  : 更新游戏房间操作
   * @return : JSON
   */
    public function  updateRoomAction(){
        $KindID = $this->request->param('KindID/d');
        $isKindID = table('GameKindItem','rydb.basic')->where('KindID',$KindID)->find();
        //return result(['is'=>$isKindID['KindName']],2,'游戏'.$KindID.'不存在');
        $isKindID2 = table('GameKindNote','rydb.websms')->where('KindID',$KindID)->count();
        $FamilyType = $this->request->param('FamilyType/d');
        $familyName = $FamilyType==1 ? '房卡区' : '金币区';
       // return result(['data'=>$familyName],2,'游戏'.$KindID.'不存在');
        if (count($isKindID) == 0){
            return result([],2,'游戏'.$KindID.'不存在');
        }else if (count($isKindID) != 0 && $isKindID2 != 0){
            return result([], 3, '游戏'.$KindID.'已经存在群组');
        }else if(count($isKindID) != 0 && $isKindID2 == 0){
            /*
             * 增加亲友团信息
             * */
            $AgentNum = table('AgentReg', 'rydb.websms')->field(['UserID', 'Compellation'])->where(['Status'=>'1'])->select();

            $code = table('CodeIdentifier', 'rydb.websms')->field('Code')->where('FamilyUsed', 0)->limit(count($AgentNum))->select();
            $cou = count($AgentNum);
            for ($i = 0; $i < $cou; $i++) {
                foreach ($code as $one) {
                    if ($AgentNum[$i]['ROW_NUMBER'] == $one['ROW_NUMBER']) {
                        $AgentNum[$i]['Codes'] = $one['Code'];
                    }
                }
                $data[] = ['AUID' => $AgentNum[$i]['UserID'], 'Code' => $AgentNum[$i]['Codes'], 'Caption' =>'['.$familyName.'].['. $isKindID['KindName']. '].'.$AgentNum[$i]['Compellation'] .'的群', 'KindID' => $KindID, 'State' => 1,'FamilyType'=>$FamilyType];

            }
            //return result(['data'=>$data], 0, '游戏'.$KindID.'success');
            $inserfamily = table('Family', 'rydb.websms')->insertAll($data);

            if (!$inserfamily) {
                return result([], 5, '亲友团增加失败');
            }

            /*
          * 增加房间号绑定
          * */
            $Users = table('Family', 'rydb.websms')->where(['KindID'=>$KindID,'FamilyType'=>$FamilyType,'State'=>1])->select();
            //dump($Users);die;
            foreach ($Users as $v) {
                $data[] = table('CodeIdentifier', 'rydb.websms')->where('Code', $v['Code'])->find();

            }
            for ($i = 0; $i < count($data); $i++) {
                foreach ($Users as $one) {
                    if ($data[$i]['Code'] == $one['Code']) {
                        $insertcodeidentifier = table('CodeIdentifier', 'rydb.websms')->where('Code', $one['Code'])->update(['FamilyUsed' => $one['ID']]);
                    }
                }
            }
            if (!$insertcodeidentifier) {
                return result([], 8, '房间号绑定失败');
            }

            /*
             * 房间权限绑定
             * */
            $Users = table('Family', 'rydb.websms')->where(['KindID'=>$KindID,'FamilyType'=>$FamilyType,'State'=>1])->select();
            foreach ($Users as $v) {
                $Familymember[] = ['FamilyID' => $v['ID'], 'MemberUID' => $v['AUID'], 'Status' => 1, 'UserRight' => 64, 'IsBlack' => 0, 'IsPass' => 1, 'Gold' => 0,'RuleNumb'=>1];
            }
            $inserfamilymember = table('FamilyMember', 'rydb.websms')->insertAll($Familymember);
            if (!$inserfamilymember) {
                return result([], 9, '房间权限修改失败');
            }
            if ($FamilyType==2) {
                $data = [
                    'KindID'      => $isKindID['KindID'],
                    'GameID'      => $isKindID['GameID'],
                    'TypeID'      => $isKindID['TypeID'],
                    'JoinID'      => $isKindID['JoinID'],
                    'SortID'      => $isKindID['SortID'],
                    'KindName'    => $isKindID['KindName'],
                    'ProcessName' => $isKindID['ProcessName'],
                    'GameRuleUrl' => $isKindID['GameRuleUrl'],
                    'DownLoadUrl' => $isKindID['DownLoadUrl'],
                    'Recommend'   => $isKindID['Recommend'],
                    'GameFlag'    => $isKindID['GameFlag'],
                    'Nullity'     => $isKindID['Nullity']
                ];
                table('GameKindNote', 'rydb.websms')->insert($data);
            }
            return result([], 0, '更新成功');

        }
    }





}
